//
//  HomeCategoryCell.m
//  PaiLeXiongProject
//
//  Created by 小獾科技 on 2017/8/21.
//  Copyright © 2017年 小獾科技. All rights reserved.
//

#import "HomeCategoryCell.h"
#import "HomeCategoryCollectionViewCell.h"
#import "HomeIconModel.h"
#import "SearchViewController.h"
@interface HomeCategoryCell ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *collectionView;

@property (nonatomic, strong) NSMutableArray *arrayModel;
@end

@implementation HomeCategoryCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
//        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        flowLayout.minimumInteritemSpacing = 0;

        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, GWIDTH, 117) collectionViewLayout:flowLayout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.backgroundColor = ColorWhite;
        [_collectionView registerClass:[HomeCategoryCollectionViewCell class] forCellWithReuseIdentifier:@"homeCategoryCell"];
        [self addSubview:_collectionView];
    }
    return self;
}

#pragma mark - CollectionViewDelegate

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.arrayModel.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    HomeCategoryCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"homeCategoryCell" forIndexPath:indexPath];
    HomeIconModel *model = _arrayModel[indexPath.row];
    cell.model = model;
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return CGSizeMake(GWIDTH / (_arrayModel.count), 117);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    HomeIconModel *model = _arrayModel[indexPath.row];
    SearchViewController *searchVC = [SearchViewController new];
    searchVC.searchKey = model.text;
    searchVC.hidesBottomBarWhenPushed = YES;
    [self.viewcontroller.navigationController pushViewController:searchVC animated:YES];
}

- (void)configWith:(NSArray *)array
{
    _arrayModel = [NSMutableArray arrayWithCapacity:0];
    for (HomeIconModel *model in array) {
        [_arrayModel addObject:model];
    }
    [_collectionView reloadData];

}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
